layui.use(['table','jquery','layer','form'],function (){
    //声明组件
    var table = layui.table;
    var $  = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;

    /*项目名称*/
    var basePath = $("#basePath").val();

    //渲染表格数据
   var tableIns = table.render({
       elem:'#classTable',//id选择器的值
        toolbar:'#classToolbar',//表头的
        url:basePath+'/findAllandmohu',
        method:'post',
        contentType:'application/json',
        titlet:'教师表',
        page:true,//开启分页
        limit:5,//分页的条数（默认的是10）
        limits:[1,2,3,4,5,6,7,8,9],//分页下拉框的选项
        cols: [
            [
                {
                    type: 'checkbox',
                    fixed: 'left',
                    width: 60
                },
                {
                    field: 'area',
                    title: '区域',
                    align: 'center',
                    width: 80,
                }, {
                field: 'buildingNo',
                title: '楼号',
                align: 'center',
                width: 140
            }, {
                field: 'buildingFloor',
                title: '楼层',
                align: 'center',
                width: 80
            }, {
                field: 'classroomNumber',
                title: '教室编号',
                align: 'center',
                width: 100
            }, {
                field: 'classroomName',
                title: '教室名称',
                align: 'center',
                width: 140
            }, {
                field: 'galleryful',
                title: '容纳人数',
                align: 'center',
                width: 80
            }, {
                field: 'useStatus',
                title: '使用状态',
                align: 'center',
                width: 120,
            },  {
                field: 'createTime',
                title: '创建时间',
                align: 'center',
                width: 160,
            }, {
                field: 'updateTime',
                title: '修改时间',
                align: 'center',
                width: 160,
            }, {
                fixed: 'right',
                width: 248,
                title: '操作',
                align: 'center',
                toolbar: '#classBar'
            }
            ]
        ],
        done: function (res, curr, count) {
            // 使用情况
            $("[data-field='useStatus']").children().each(function () {
                if ($(this).text() == '0') {
                    $(this).text('使用');
                } else if ($(this).text() == '1') {
                    $(this).text('未使用');
                }else if ($(this).text() == '2') {
                    $(this).text('维修中');
                }
            });

            $("[data-field='area']").children().each(function () {
                if ($(this).text() == 'A') {
                    $(this).text('A区');
                } else if ($(this).text() == 'B') {
                    $(this).text('B区');
                } else if ($(this).text() == 'C') {
                    $(this).text('C区');
                } else if ($(this).text() == 'D') {
                    $(this).text('D区');
                } else if ($(this).text() == 'E') {
                    $(this).text('E区');
                }
            });
            $("[data-field='buildingFloor']").children().each(function () {
                if ($(this).text() == '1') {
                    $(this).text('第一层');
                } else if ($(this).text() == '2') {
                    $(this).text('第三层');
                } else if ($(this).text() == '3') {
                    $(this).text('第三层');
                }
            });
            $("[data-field='buildingNo']").children().each(function () {
                if ($(this).text() == '1') {
                    $(this).text('第一号楼');
                } else if ($(this).text() == '2') {
                    $(this).text('第二号楼');
                } else if ($(this).text() == '3') {
                    $(this).text('第三号楼');
                }
            });

            $("[data-field='createTime']").children().each(function () {
                var birthday = $(this).text();
                if (birthday.indexOf('创建时间') == -1) {
                    $(this).text(getYMDHMS(parseInt(birthday)));
                }
            });

            $("[data-field='updateTime']").children().each(function () {
                var birthday = $(this).text();
                if (birthday.indexOf('修改时间') == -1) {
                    $(this).text(getYMDHMS(parseInt(birthday)));
                }
            });

        }
    });

   //教室的下拉框
    $.ajax({
        type:"post",
        url:basePath+"/threexlk",
        contentType:"application/json;charset=utf-8",
        dataType:"json",
        success:function (alist) {
            var ahtml = "<option value=''>请选择区域</option>";
            for (var i = 0; i < alist.length; i++) {
                ahtml += "<option value="+alist[i].area+">"+alist[i].area+"区</option>";
            }
            layer.msg(ahtml);
            $("select[name='area']").html(ahtml);
            form.render('select');  //渲染下拉框
        },error:function () {
            layer.msg("请求失败1");
        }

    });

    //楼号下拉框
    form.on('select(area)',function (data) {

        console.log(data.elem);
         console.info(data.value);

         $.ajax({
             type:"post",
             url:basePath+"/threexlkbn",
             contentType:"application/json;charset=utf-8",
             data:JSON.stringify({area:data.value}),
             dataType:"json",
             success:function (bnlist) {
                 //itar
                 var bnhtml = "<option value=''>请选择楼号</option>";
                 for (var i = 0; i < bnlist.length; i++) {
                     bnhtml +="<option value="+bnlist[i].buildingNo+">第"+bnlist[i].buildingNo+"号楼</option>";
                 }
                 layer.msg(bnhtml);
                 $("select[name='buildingNo']").html(bnhtml);
                 form.render('select'); //渲染下拉框
             },error:function (){
                 layer.msg("请求失败2");
             }

         });

    });


    //楼层的下拉框
    form.on('select(buildingNo)',function (data) {

        console.info(data.value);

        $.ajax({
            type:"post",
            url: basePath+"/threexlkbf",
            contentType:"application/json;charset=utf-8",
            data:JSON.stringify({area:$("select[name='area']").val(),buildingNo:data.value}),
            dataType:"json",
            success:function (bflist) {

                var  bfhtml = "<option value=''>请选择楼层</option>";
                for (var i = 0; i < bflist.length; i++) {
                    bfhtml +="<option value="+bflist[i].buildingFloor+">第"+bflist[i].buildingFloor+"楼层</option>";
                }
                layer.msg(bfhtml);
                console.info(bfhtml);
                $("select[name='buildingFloor']").html(bfhtml);
                form.render('select');
            },error:function () {
                    layer.msg("请求失败3");
                }
        });
    });





    //模糊查询
    form.on("submit(fuzzyQuery)",function (data){
        console.info(data.field);

        tableIns.reload({
            where:data.field,   //查询条件
           page: {
                curr:1,     //起始页。一般用于刷新类型的跳页
           }
        });
        return false;   //组织表单跳转
    });






});










// 时间戳
function getYMDHMS(timestamp) {
    let time = new Date(timestamp)
    let year = time.getFullYear()
    const month = (time.getMonth() + 1).toString().padStart(2, '0')
    const date = (time.getDate()).toString().padStart(2, '0')
    const hours = (time.getHours()).toString().padStart(2, '0')
    const minute = (time.getMinutes()).toString().padStart(2, '0')
    const second = (time.getSeconds()).toString().padStart(2, '0')

    return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}